<?php
namespace Home\Controller;
use Think\Controller;
// use Common\Controller\QianController;//继承QianController，设置权限
class IndexController extends Controller {
    public function _initialize(){//初始化。该控制器里面所有的方法都能得到top_banner_left数组
        $user_id=session('user_id');
        $username=session('username');
        $this->assign('username',$username);
        $this->assign('user_id',$user_id);

        $this->loginform=U('Login/LoginForm');
        $this->myorder=U('User/Myorder');
        $r=$this->getCateByPid();
        $this->assign('top_banner_left',$r);
        $this->assign('logout',U('Login/logout'));//index控制器退出登录
        $this->assign('register',U('Login/RegisForm'));
        $this->assign('tobuycar',U('User/Buycar'));
        $this->assign('index',U('Index/Index'));
        $this->productlist=U('ProductList');
        header('Content-type:text/html;charset=utf-8');
    }

    public function Index(){
        // echo session('user_id');
        // exit;
        $this->product=U('Product');
        $this->productlist=U('ProductList');//点击分类下面的分类名称，调到相关商品ID商品展示页面
    	$user=M('goods');
    	$goods=$user->select();
    	$this->assign('goods',$goods);
    	$one=$user->order('price desc')->find();//热门商品行，最左边有一个，选择商品中最贵的最贵的一个
    	$this->assign('one',$one);
    	$picid=$one['id'];//图片的id,在照片表里面取响应的照片；
    	$p=M('photo');
    	$pic=$p->where("goods_id='{$picid}'")->find();
    	$this->assign('pic',$pic);
    	$result=$user->join('inner join fm_photo on fm_goods.id=fm_photo.goods_id')->limit(10)->select();
        //最上热门，商品为选出所有的商品；
        shuffle($result);
        $result=array_slice($result,0,8);
    	$this->assign('result',$result);
        $cate=M('category');
        $phone=$cate->where("catename like '手机'")->find();
        $phid=$phone['id'];//此ID为父亲ID耳机区域，选出所有二级，选择两个
        $phone=$user->join("left join fm_goods_category on fm_goods.id=fm_goods_category.goods_id left join fm_category on fm_goods_category.category_id=fm_category.id left join fm_photo on fm_goods.id=fm_photo.goods_id")->where("precategory='{$phid}'")->order('price asc')->select();
        $this->assign('phone',$phone);
        shuffle($phone);
        $phone=array_slice($phone,0,2);//先乱序，截取两个数组限时特卖最左侧两张轮播图
        $this->assign('re',$phone);
        $c=$cate->where("catename like '耳机'")->find();
        $cid=$c['id'];//此ID为父亲ID耳机区域，选出所有二级，选择两个
        $t=$user->join("left join fm_goods_category on fm_goods.id=fm_goods_category.goods_id left join fm_category on fm_goods_category.category_id=fm_category.id left join fm_photo on fm_goods.id=fm_photo.goods_id")->where("precategory='{$cid}'")->order('price asc')->select();
        $this->assign('t',$t);
        shuffle($t);
        $tt=array_slice($t,0,2);//先乱序，截取两个数组
        // print_r($tt);
        $this->assign('tt',$tt);

        $com=$cate->where("catename like '电脑'")->find();
        $comid=$com['id'];//此ID为父亲ID耳机区域，选出所有二级，选择两个
        $com=$user->join("left join fm_goods_category on fm_goods.id=fm_goods_category.goods_id left join fm_category on fm_goods_category.category_id=fm_category.id left join fm_photo on fm_goods.id=fm_photo.goods_id")->where("precategory='{$comid}'")->order('price asc')->select();
        shuffle($com);
        $com=array_slice($com,0,1);//先乱序，截取1个数组
        $this->assign('com',$com);
        //************************中间手机列表6个*********************************
        $cenphone=$cate->join('left join fm_goods_category on fm_category.id=fm_goods_category.category_id')->where("catename like '手机'")->select();
        $goodsid=array();
        foreach ($cenphone as $key => $value) {
           $goodsid[]=$value['goods_id'];
        }
        $goodsid=implode(',', $goodsid);
        $total=$user->field('fm_goods .*,(select name from fm_photo where fm_goods.id=fm_photo.goods_id limit 1) as name ')->where("id in ({$goodsid})")->select();
        // print_r($total);
        // exit;
         $this->assign('cenphone',$total);
         //************************中间第二排电脑6个*********************************
        $cencom=$cate->join('left join fm_goods_category on fm_category.id=fm_goods_category.category_id')->where("catename like '电脑'")->select();
        // print_r($cencom);
        // exit;
        $comid=array();
        foreach ($cencom as $key => $value) {
           $comid[]=$value['goods_id'];
        }
        $cencom=implode(',',$comid);
        $total=$user->field('fm_goods .*,(select name from fm_photo where fm_goods.id=fm_photo.goods_id limit 1) as name ')->where("id in ({$cencom})")->select();
        // print_r($total);
        // exit;
        $this->assign('comcen',$total);
        $this->display(); 
    }
     public function getCateByPid ($pid=0){//递归函数去值
		$user=M('Category');
		$result=$user->where("status=1 and precategory={$pid}")->select();
		if(!empty($result)){
			foreach ($result as $key => $value) {
				$result[$key]['child']=$this->getCateByPid($value['id']);
			}
		}
		return $result;
    }
    public function ProductList(){
        layout('user');
        $this->assign('buycar',U('Index/Tobuycar'));//AJAX跳转到TOBUYCAR进行处理
        $this->assign('listtocar',U('Index/Listtocar'));//AJAX跳转到TOBUYCAR进行处理
        if(!is_null(I('get.cateid'))){
            $cateid=I('get.cateid');//通过分类ID获得商品ID。一个分类下面有多个商品
            $cate=M('category');
            $goods=M('goods');
            $r=$cate->join('left join fm_goods_category on fm_category.id=fm_goods_category.category_id')->where("category_id='{$cateid}'")->select();
            $goodsid=array();
            foreach ($r as $key => $value) {
               $goodsid[]=$value['goods_id'];
            }
            $goodsid=implode(',', $goodsid);
            $total=$goods->field('fm_goods .*,(select name from fm_photo where fm_goods.id=fm_photo.goods_id limit 1) as name ')->where("id in ({$goodsid})")->select();
            $this->assign('cenphone',$total);//小分类下的所有商品全部展示，分页。
            $link=$cate->field('catename')->where("id='{$cateid}'")->find();
            $this->assign('catename',$link);//商品总分类名称
            // print_r($total);
            // exit;
            $this->assign('cenphone',$total);
            shuffle($total);
            $love=array_slice($total,0,4);
            $this->assign('love',$love);
            $this->assign('product',U('Product'));
            $this->display();
        }else{
            $this->error('操作失败',U('Index/Index'));
        }
    }

    public function Product(){
        layout('user');
        $this->assign('buycar',U('Index/Tobuycar'));//AJAX跳转到TOBUYCAR进行处理
        // $this->assign('productlist',U('ProductList'));
        // $this->assign('buycar',U('Tobuycar'));//跳转页面，加入购物车操作
        if(!is_null(I('get.goodsid'))){
            $goodsid=I('get.goodsid');
            $goods=M('goods');
            $goodsshow=$goods->join("left join fm_photo on fm_goods.id=fm_photo.goods_id")->where("fm_goods.id='{$goodsid}'")->find();
            $this->assign('product',$goodsshow);
            // print_r($goodsshow);
            // exit;
            $p=M('photo');
            $photo=$p->where("goods_id='{$goodsid}'")->select();
            // print_r($photo);
            // exit;
            $this->assign('photo',$photo);
            $q=M('goods_category');
            $w=$q->where("goods_id='{$goodsid}'")->select();//此页面用户还喜欢的商品
            $c=array();
            foreach ($w as $key => $value) {
                $c[]=$value['category_id'];
            }
            sort($c);
            $cate_id=$c[0];
            $big=$goods->field('fm_goods .*,(select name from fm_photo where fm_goods.id=fm_photo.goods_id limit 1) as name ')->join("left join fm_goods_category on fm_goods.id=fm_goods_category.goods_id")->where("category_id='{$cate_id}'")->select();
            shuffle($big);//数组随机乱序
            $andlove=array_slice($big,0,3);
            // print_r($andlove);
            // exit;
            $this->assign('andlove',$andlove);
            $this->display();
        }else{
            $this->error('操作失败',U('Index/Index'));
        }
    }

    public function Tobuycar(){//加入购车
        $loginuser_id=$this->user_id;//登录时，保存的用户ID    
        if(IS_AJAX){
            $user=M('buycar');
            // $num=$user->where("loginuser_id='{$loginuser_id}'")->select();
            // $num=count($num);
            // if($num>5){
            //     $this->ajaxreturn(array('code'=>'000','msg'=>'购物车最多加入六个'));
            // }
            $goodsid=I('post.goodsid');
            $over=$user->where("loginuser_id='{$loginuser_id}'")->getField('goods_id',true);
            if(in_array($goodsid, $over)){
                $this->ajaxreturn(array('code'=>'000','msg'=>'该商品已加入购物车'));
            }
            // print_r($over);
            // exit;
            $totalpay=I('post.totalpay');
            $data=array(
                'loginuser_id'=>$loginuser_id,
                'goods_id'=>$goodsid,
                'add_time'=>date("Y-m-d H:i:s",time()),
                'totalpay'=>$totalpay
                );
            // print_r($data);
            // exit;

            $r=$user->add($data);
            if($r){
                $this->ajaxreturn(array('code'=>'666','msg'=>'加入成功'));
            }else{
                $this->ajaxreturn(array('code'=>'000','msg'=>'加入失败 '));
            }
        }else{
            $this->error('操作失败',U('Index/Index'));
        }
    }

    
    
}

   //*****************************************************************
            // $gtc=M('category');
            // $tid=$gtc->where("id='{$cateid}'")->find();
            // $pid=$tid['precategory'];
            // $g=$gtc->where("id='{$pid}'")->find();
            // $bigid=$g['id'];//父亲ID
            // // exit;
            // // echo $bigid;//大分类下所有的商品展示
            // $bigtotal=$goods->field('fm_goods .*,(select name from fm_photo where fm_goods.id=fm_photo.goods_id limit 1) as name ')->join("left join fm_goods_category on fm_goods.id=fm_goods_category.goods_id")->where("category_id='{$bigid}'")->select();
            // $this->assign('cenphone',$bigtotal);
            // // exit;
            // print_r($bigtotal);
            // // print_r($total);
            // exit;
            //***********************23.01*******************************************
// public function Listtocar(){//此方法为Productlist页面及方法，功能暂时关闭
    //     $loginuser_id=$this->user_id;//登录时，保存的用户ID
    //     // $cateid=I('get.cateid');
    //     if(!is_null(I('get.gid'))){
    //         $goodsid=I('get.gid');
    //         $user=M('buycar');
    //         $num=$user->where("loginuser_id='{$loginuser_id}'")->select();
    //         $num=count($num);
    //         // print_r($num);
    //         // exit;
    //         if($num<7){
    //             $data=array();
    //             $data=array(
    //             'loginuser_id'=>$loginuser_id,
    //             'goods_id'=>$goodsid,
    //             'add_time'=>date("Y-m-d H:i:s",time())
    //             );
    //             $r=$user->add($data);
    //             if($r){
    //                 $this->success('加入成功',"ProductList?cateid=4");
    //             }else{
    //                 $this>error('操作失败');
    //             }
    //         }else{
    //             $this->error('购物车最多加555入六个');
    //         }
            
    //     }
    // }